************** this stata script replicates the results presented in tables S4-S7 in supplementary materials (figure 2 in main manuscript) *************


***** same day exercise influence ******
clear all
import delimited "ego_level_same_day_corr_thresh_0025_same_day.txt" 



*define the dependent variable, the endogenous effect, the instruments and the controls

global distance_dependent ego_distance_t
global distance_endogenous av_fr_distance_t
global distance_instruments fr_rain_7_t fr_tmax_2_t
global distance_control_variables ego_distance_t_1 ego_tmax_t ego_prcp_t ego_connectivity_t av_fr_connectivity_t av_fr_age av_fr_age av_fr_weight portion_of_friends_that_male portion_of_friends_US portion_of_friends_UK portion_of_friends_JP portion_of_friends_CA portion_of_friends_wristdevice portion_of_friends_application1 portion_of_friends_application2 


global duration_dependent ego_duration_t
global duration_endogenous av_fr_duration_t
global duration_instruments fr_rain_7_t fr_tmax_3_t
global duration_control_variables ego_duration_t_1 ego_tmax_t ego_prcp_t ego_connectivity_t av_fr_connectivity_t av_fr_age av_fr_age av_fr_weight portion_of_friends_that_male portion_of_friends_US portion_of_friends_UK portion_of_friends_JP portion_of_friends_CA portion_of_friends_wristdevice portion_of_friends_application1 portion_of_friends_application2


global pace_dependent ego_pace_t
global pace_endogenous av_fr_pace_t
global pace_instruments fr_rain_3_t fr_tmax_3_t
global pace_control_variables ego_pace_t_1 ego_tmax_t ego_prcp_t ego_connectivity_t av_fr_connectivity_t av_fr_age av_fr_age av_fr_weight portion_of_friends_that_male portion_of_friends_US portion_of_friends_UK portion_of_friends_JP portion_of_friends_CA portion_of_friends_wristdevice portion_of_friends_application1 portion_of_friends_application2 


global calories_dependent ego_calories_t
global calories_endogenous av_fr_calories_t
global calories_instruments fr_rain_7_t fr_tmax_3_t
global calories_control_variables ego_calories_t_1 ego_tmax_t ego_prcp_t ego_connectivity_t av_fr_connectivity_t av_fr_age av_fr_age av_fr_weight portion_of_friends_that_male portion_of_friends_US portion_of_friends_UK portion_of_friends_JP portion_of_friends_CA portion_of_friends_wristdevice portion_of_friends_application1 portion_of_friends_application2 


*distance
xi: ivreg2  $distance_dependent  ( $distance_endogenous =   $distance_instruments )  $distance_control_variables i.ego_num i.day_num , first small cluster(community_id) endog($distance_endogenous)


*note that you can run the following *equivalent* command:
xtset ego_num day_num *set up the panel
xtivreg2 $distance_dependent  ( $distance_endogenous =   $distance_instruments )  $distance_control_variables i.day_num, first small cluster(community_id) endog($distance_endogenous)



*duration
xi: ivreg2  $duration_dependent  ( $duration_endogenous =   $duration_instruments )  $duration_control_variables  i.ego_num i.day_num , first small cluster(community_id) endog($distance_endogenous)

*pace
xi: ivreg2  $pace_dependent  ( $pace_endogenous =   $pace_instruments )  $pace_control_variables  i.ego_num i.day_num ,  first small cluster(community_id) endog($pace_endogenous)

*calories burned
xi: ivreg2  $calories_dependent  ( $calories_endogenous =   $calories_instruments )  $calories_control_variables  i.ego_num i.day_num , first small cluster(community_id) endog($calories_endogenous)



***** one day diff. exercise influence ******
clear all
import delimited "ego_level_same_day_corr_thresh_0025_one_day.txt" 


*define the dependent variable, the endogenous effect, the instruments and the controls

global distance_dependent ego_distance_t
global distance_endogenous av_fr_distance_t_1
global distance_instruments fr_rain_7_t_1 fr_tmax_2_t_1
global distance_control_variables ego_distance_t_1 ego_distance_t_2 ego_tmax_t ego_prcp_t ego_connectivity_t_1 av_fr_connectivity_t_1 av_fr_age av_fr_age av_fr_weight portion_of_friends_that_male portion_of_friends_US portion_of_friends_UK portion_of_friends_JP portion_of_friends_CA portion_of_friends_wristdevice portion_of_friends_application1 portion_of_friends_application2 


global duration_dependent ego_duration_t
global duration_endogenous av_fr_duration_t_1
global duration_instruments fr_rain_7_t_1 fr_tmax_3_t_1
global duration_control_variables ego_duration_t_1 ego_duration_t_2 ego_tmax_t ego_prcp_t ego_connectivity_t_1 av_fr_connectivity_t_1 av_fr_age av_fr_age av_fr_weight portion_of_friends_that_male portion_of_friends_US portion_of_friends_UK portion_of_friends_JP portion_of_friends_CA portion_of_friends_wristdevice portion_of_friends_application1 portion_of_friends_application2 


global pace_dependent ego_pace_t
global pace_endogenous av_fr_pace_t_1
global pace_instruments fr_rain_3_t_1 fr_tmax_3_t_1
global pace_control_variables ego_pace_t_1 ego_pace_t_2 ego_tmax_t ego_prcp_t ego_connectivity_t_1 av_fr_connectivity_t_1 av_fr_age av_fr_age av_fr_weight portion_of_friends_that_male portion_of_friends_US portion_of_friends_UK portion_of_friends_JP portion_of_friends_CA portion_of_friends_wristdevice portion_of_friends_application1 portion_of_friends_application2 


global calories_dependent ego_calories_t
global calories_endogenous av_fr_calories_t_1
global calories_instruments fr_rain_7_t_1 fr_tmax_3_t_1
global calories_control_variables ego_calories_t_1 ego_calories_t_2 ego_tmax_t ego_prcp_t ego_connectivity_t_1 av_fr_connectivity_t_1 av_fr_age av_fr_age av_fr_weight portion_of_friends_that_male portion_of_friends_US portion_of_friends_UK portion_of_friends_JP portion_of_friends_CA portion_of_friends_wristdevice portion_of_friends_application1 portion_of_friends_application2 


*distance
xi: ivreg2  $distance_dependent  ( $distance_endogenous =   $distance_instruments )  $distance_control_variables  i.ego_num i.day_num , first small cluster(community_id) endog($distance_endogenous)

*duration
xi: ivreg2 $duration_dependent  ( $duration_endogenous =   $duration_instruments )  $duration_control_variables  i.ego_num i.day_num , first small cluster(community_id) endog($distance_endogenous)

*pace
xi: ivreg2  $pace_dependent  ( $pace_endogenous =   $pace_instruments )  $pace_control_variables  i.ego_num i.day_num , first small cluster(community_id) endog($pace_endogenous)

*calories burned
xi: ivreg2  $calories_dependent  ( $calories_endogenous =   $calories_instruments )  $calories_control_variables i.ego_num i.day_num , first small cluster(community_id) endog($calories_endogenous)


***** two days diff. exercise influence ******
clear all
import delimited "ego_level_same_day_corr_thresh_0025_two_day.txt" 


*define the dependent variable, the endogenous effect, the instruments and the controls

global distance_dependent ego_distance_t
global distance_endogenous av_fr_distance_t_2
global distance_instruments fr_rain_7_t_2 fr_tmax_2_t_2
global distance_control_variables ego_distance_t_1 ego_distance_t_2 ego_distance_t_3 ego_tmax_t ego_prcp_t ego_connectivity_t_2 av_fr_connectivity_t_2  av_fr_age av_fr_age av_fr_weight portion_of_friends_that_male portion_of_friends_US portion_of_friends_UK portion_of_friends_JP portion_of_friends_CA portion_of_friends_wristdevice portion_of_friends_application1 portion_of_friends_application2 


global duration_dependent ego_duration_t
global duration_endogenous av_fr_duration_t_2
global duration_instruments fr_rain_7_t_2 fr_tmax_3_t_2
global duration_control_variables ego_duration_t_1 ego_duration_t_2 ego_duration_t_3 ego_tmax_t ego_prcp_t ego_connectivity_t_2 av_fr_connectivity_t_2  av_fr_age av_fr_age av_fr_weight portion_of_friends_that_male portion_of_friends_US portion_of_friends_UK portion_of_friends_JP portion_of_friends_CA portion_of_friends_wristdevice portion_of_friends_application1 portion_of_friends_application2 


global pace_dependent ego_pace_t
global pace_endogenous av_fr_pace_t_2
global pace_instruments fr_rain_3_t_2 fr_tmax_3_t_2
global pace_control_variables ego_pace_t_1 ego_pace_t_2 ego_pace_t_3 ego_tmax_t ego_prcp_t ego_connectivity_t_2 av_fr_connectivity_t_2  av_fr_age av_fr_age av_fr_weight portion_of_friends_that_male portion_of_friends_US portion_of_friends_UK portion_of_friends_JP portion_of_friends_CA portion_of_friends_wristdevice portion_of_friends_application1 portion_of_friends_application2 


global calories_dependent ego_calories_t
global calories_endogenous av_fr_calories_t_2
global calories_instruments fr_rain_7_t_2 fr_tmax_3_t_2
global calories_control_variables ego_calories_t_1 ego_calories_t_2 ego_calories_t_3 ego_tmax_t ego_prcp_t ego_connectivity_t_2 av_fr_connectivity_t_2  av_fr_age av_fr_age av_fr_weight portion_of_friends_that_male portion_of_friends_US portion_of_friends_UK portion_of_friends_JP portion_of_friends_CA portion_of_friends_wristdevice portion_of_friends_application1 portion_of_friends_application2 


*distance
xi: ivreg2  $distance_dependent  ( $distance_endogenous =   $distance_instruments )  $distance_control_variables  i.ego_num i.day_num , first small cluster(community_id) endog($distance_endogenous)

*duration
xi: ivreg2  $duration_dependent  ( $duration_endogenous =   $duration_instruments )  $duration_control_variables  i.ego_num i.day_num , first small cluster(community_id) endog($distance_endogenous)

*pace
xi: ivreg2  $pace_dependent  ( $pace_endogenous =   $pace_instruments )  $pace_control_variables  i.ego_num i.day_num , first small cluster(community_id) endog(pace_endogenous)

*calories burned
xi: ivreg2  $calories_dependent  ( $calories_endogenous =   $calories_instruments )  $calories_control_variables  i.ego_num i.day_num , first small cluster(community_id) endog($calories_endogenous)

